package jezyk_java.robert_wolos.watki;



/**
 * Autor: Daniel Michalski
 * Data: 11.07.13
 */

public class Q {
    private int n;
    private boolean valueSet = false;

    public synchronized int get() {
        if (!valueSet) {
            try {
                wait();
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                System.out.println("Złapano InterruptedException");
            }
        }

        System.out.println("Pobrano " + n);
        valueSet = false;
        notify();
        return n;
    }

    synchronized void put (int n) {
        if (valueSet) {
            try {
                wait();
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                System.out.println("Złapano InterruptedException");
            }
        }

        this.n = n;
        valueSet = true;
        System.out.println("Włożono " + n);
        notify();
    }
}
